package com.bh.shipment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.constant.Entity;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.shipment.domain.JcShipmentTrack;
import com.bh.shipment.mapper.JcShipmentTrackMapper;
import com.bh.shipment.service.IJcShipmentTrackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 运单追踪Service业务层处理
 *
 * @author slx
 * @date 2023-04-22
 */

@Service
public class JcShipmentTrackServiceImpl extends ServiceImpl<JcShipmentTrackMapper, JcShipmentTrack> implements IJcShipmentTrackService {
    @Autowired
    private JcShipmentTrackMapper jcShipmentTrackMapper;

    public static final String HR = "-------------------------------------------------------------------------------\n";
    /**
     * 查询运单追踪
     *
     * @param id 运单追踪主键
     * @return 运单追踪
     */
    @Override
    public JcShipmentTrack selectJcShipmentTrackById(Long id) {
        return jcShipmentTrackMapper.selectById(id);
    }

    /**
     * 查询运单追踪列表
     *
     * @param jcShipmentTrack 运单追踪
     * @return 运单追踪
     */
    @Override
    public List<JcShipmentTrack> selectJcShipmentTrackList(JcShipmentTrack jcShipmentTrack) {
        return jcShipmentTrackMapper.selectJcShipmentTrackList(jcShipmentTrack);
    }

    /**
     * 新增运单追踪
     *
     * @param jcShipmentTrack 运单追踪
     * @return 结果
     */
    @Override
    public int insertJcShipmentTrack(JcShipmentTrack jcShipmentTrack) {
        jcShipmentTrack.setStatus(Entity.ACTIVE);
        if (jcShipmentTrack.getPerson() == null) {
            jcShipmentTrack.setPerson(SecurityUtils.getUsername());
        }
        if (jcShipmentTrack.getTime() == null) {
            jcShipmentTrack.setTime(DateUtils.getNowDate());
        }
        if (jcShipmentTrack.getType() == null) {
            jcShipmentTrack.setType(0);
        }
        jcShipmentTrack.setCreateName(SecurityUtils.getUsername());
        jcShipmentTrack.setCreateTime(DateUtils.getNowDate());
        return jcShipmentTrackMapper.insert(jcShipmentTrack);
    }

    @Override
    public int addTrack(Long shipmentId, String ... eventAndImgs) {
        JcShipmentTrack shipmentTrack = new JcShipmentTrack();
        shipmentTrack.setJcShipmentId(shipmentId);
        shipmentTrack.setTime(DateUtils.getNowDate());
        shipmentTrack.setCreateTime(new Date());
        shipmentTrack.setEvent(eventAndImgs[0]);
        if (eventAndImgs.length > 1) {
            shipmentTrack.setPicture(eventAndImgs[1]);
        }
        shipmentTrack.setType(0);
        shipmentTrack.setCreateName(SecurityUtils.getUsername());
        shipmentTrack.setPerson(SecurityUtils.getUsername());
        shipmentTrack.setStatus(1);
        return jcShipmentTrackMapper.insert(shipmentTrack);
    }

    /**
     * 修改运单追踪
     *
     * @param jcShipmentTrack 运单追踪
     * @return 结果
     */
    @Override
    public int updateJcShipmentTrack(JcShipmentTrack jcShipmentTrack) {
        return jcShipmentTrackMapper.updateById(jcShipmentTrack);
    }

    /**
     * 批量删除运单追踪
     *
     * @param ids 需要删除的运单追踪主键
     * @return 结果
     */
    @Override
    public int deleteJcShipmentTrackByIds(Long[] ids) {
        return jcShipmentTrackMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除运单追踪信息
     *
     * @param id 运单追踪主键
     * @return 结果
     */
    @Override
    public int deleteJcShipmentTrackById(Long id) {
        return jcShipmentTrackMapper.deleteById(id);
    }

    @Override
    public int deleteJcShipmentTrackByShipmentId(Long shipmentId) {
        return jcShipmentTrackMapper.deleteByShipmentId(shipmentId);
    }

    @Override
    public String getTrackForString(Long shipmentId) {
        List<JcShipmentTrack> shipmentTracks = jcShipmentTrackMapper.selectJcShipmentTrackListByShipmentId(shipmentId);
        StringBuilder sb = new StringBuilder();
        for (JcShipmentTrack shipmentTrack : shipmentTracks) {
            sb.append("跟踪时间:").append(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, shipmentTrack.getTime())).append("\n ");
            sb.append("操作人:").append(shipmentTrack.getPerson()).append("\n ");
            sb.append("事件:").append(shipmentTrack.getEvent()).append("\n ");
            sb.append(HR);
        }
        return sb.toString();
    }
}
